import { userApi } from '../../services/api';

Page({
  data: {
    boxInfo: null,
    loading: true
  },

  onLoad() {
    this.loadBoxInfo();
  },

  onPullDownRefresh() {
    this.loadBoxInfo().then(() => {
      wx.stopPullDownRefresh();
    });
  },

  async loadBoxInfo() {
    try {
      this.setData({ loading: true });
      const profile = await userApi.getProfile();
      
      if (profile) {
        this.setData({
          boxInfo: {
            status: 1,
            info: profile
          }
        });
      } else {
        this.setData({
          boxInfo: {
            status: 0,
            info: null
          }
        });
      }

    } catch (err) {
      console.error('获取盒子信息失败', err);
      wx.showToast({
        title: '获取信息失败',
        icon: 'none'
      });
    } finally {
      this.setData({ loading: false });
    }
  },

  handleCreateBox() {
    wx.navigateTo({
      url: '/pages/profile/profile'
    });
  }
}); 